package com.pap.rbac.userrole.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.obj.vo.response.ResponseVO;
import com.pap.rbac.agent.userrole.IUserRoleRelAgent;
import com.pap.rbac.agent.userrole.dto.UserRoleRelDTO;
import com.pap.rbac.userrole.auto.entity.UserRoleRel;

@RestController
@RequestMapping("/v1/rbac/userrolerel")
public class UserRoleRelController extends BaseController<UserRoleRel, IUserRoleRelAgent> {

	private static Logger logger  =  LoggerFactory.getLogger(UserRoleRelController.class);
	
	@Resource(name = "userRoleRelAgent")
	@Override
	public void setService(IUserRoleRelAgent service) {
		super.service = service;
	}

	@RequestMapping(value = "/saverels")
	public ResponseVO<UserRoleRel> saveRels(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "saveRelsObj", required = true) String saveRelsObj) throws Exception {
		
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		UserRoleRelDTO userRoleRelDTO = objectMapper.readValue(saveRelsObj, UserRoleRelDTO.class);
		System.out.println(userRoleRelDTO.toString());
		service.updateUserRoleRel(clientLicenseId, userRoleRelDTO.getUserId(), userRoleRelDTO.getRoleIds());
		return ResponseVO.successdata("操作成功");
	}
	
	@RequestMapping(value = "/selectroleidsbyuserid")
	public ResponseVO<UserRoleRel> selectRoleIdsByUserId(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "userObj", required = true) String userObj) throws Exception {
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		UserRoleRelDTO userRoleRelDTO = objectMapper.readValue(userObj, UserRoleRelDTO.class);

		List<String> roleIdsList = service.selectUserRoleIds(userRoleRelDTO.getUserId());
		
		return ResponseVO.successdatas(roleIdsList, null);
	}

}
